
/*
 * @lc app=leetcode.cn id=3 lang=java
 *
 * [3] 无重复字符的最长子串
 */

// @lc code=start
class Solution {

    // 优化的滑动窗口
    // [5ms, 61%], [41.5MB, 45%]
    public int lengthOfLongestSubstring(String s) {
        int len = s.length();
        int maxLen = 0;
        int startIndex = 0;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            String sub = s.substring(startIndex, i);
            int indexAt = sub.indexOf(String.valueOf(c));
            if (indexAt == -1) {
                maxLen = Math.max(maxLen, i + 1 - startIndex);
            } else {
                startIndex = startIndex + indexAt + 1;
            }
        }
        return maxLen;
    }
}
// @lc code=end
